package com.cos.lession72.slice;

import com.cos.lession72.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ImgAbilitySlice extends AbilitySlice {
    int count=0;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_img);
        //把现有的图片资源保存集合
        List<Integer> imgIds=new ArrayList<>();
        imgIds.add(ResourceTable.Media_img_1);
        imgIds.add(ResourceTable.Media_img_2);
        imgIds.add(ResourceTable.Media_img_3);
        imgIds.add(ResourceTable.Media_img_4);
        imgIds.add(ResourceTable.Media_img_5);
        Button btn1 = (Button) findComponentById(ResourceTable.Id_changeImg);
        Image img= (Image) findComponentById(ResourceTable.Id_imgPic);
        btn1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                count++;
                if (count>=imgIds.size()){
                    count=0;
                }
                int i = new Random().nextInt(imgIds.size());

                //提供图片的资源id,他就会自己去解析设置图片
                img.setImageAndDecodeBounds(imgIds.get(count));
            }
        });

    }
}
